package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.RepairOrderDao;
import cn.gson.crm.model.dao.RepairOrderDetailsDao;
import cn.gson.crm.model.mapper.ExchangegoodsMapper;
import cn.gson.crm.model.mapper.RepairOrderMapper;
import cn.gson.crm.model.mapper.ReturngoodsMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class RepairOrderService {
    @Autowired
    RepairOrderMapper repairOrderMapper;
    @Autowired
    RepairOrderDao repairOrderDao;
    @Autowired
    RepairOrderDetailsDao repairOrderDetailsDao;
    @Autowired
    ReturngoodsMapper returngoodsMapper;
    @Autowired
    ExchangegoodsMapper exchangegoodsMapper;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //查询当前登录人维修工单的
    public Map<String,Object> selectrep(HttpSession session,Integer rid,Integer page,Integer size,String jinnian,String qunian,String qiannian){
        rid=fouyuan(session);
        List<RepairOrder> repairOrders2=repairOrderMapper.selectrep(rid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<RepairOrder> repairOrders=repairOrderMapper.selectrep(rid);
        //订单号
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        //查询当前人下所有客户
        //统计图时间查询数据
        List<RepairOrder> contractList4=repairOrderMapper.selectnian(rid,jinnian);
        List<RepairOrder> contractList5=repairOrderMapper.selectnian(rid,qunian);
        List<RepairOrder> contractList6=repairOrderMapper.selectnian(rid,qiannian);
        User login = (User)session.getAttribute("login");
        int ygId= (int) login.getEmployees().get(0).getYgId();
        List<Customer> customerList=repairOrderMapper.selectcustomer(ygId);
       List<ReturnGoods> returnGoods=returngoodsMapper.selectret();
       List<ExchangeGoods> exchangeGoods=exchangegoodsMapper.selectexc();
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",repairOrders);
        map.put("number","WX"+newDate+result);
        map.put("customerList",customerList);
        map.put("jinnian",contractList4);
        map.put("qunian",contractList5);
        map.put("qiannian",contractList6);
        map.put("tong",repairOrders2);
        map.put("returnGoods",returnGoods);
        map.put("exchangeGoods",exchangeGoods);
        return map;

    };
    //多条件查询维修记录
    public Map<String,Object> selectwhere(HttpSession session,Integer rid, String where,Integer page,Integer size){
        rid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<RepairOrder> repairOrderList=repairOrderMapper.selectwhere(rid,where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",repairOrderList);
        return map;
    };
    //时间查询维修记录
    public Map<String,Object> selecttime(HttpSession session,Integer rid,String statr,String endtime,Integer page,Integer size){
        rid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<RepairOrder> repairOrderList=repairOrderMapper.selecttime(rid,statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",repairOrderList);
        return map;
    };
    //新增维修工单
    public void save(RepairOrder repairOrder){
        if(repairOrder.getRepairId()>0){
            repairOrderDao.save(repairOrder);
        }else{
            repairOrderDao.save(repairOrder);
            repairOrder.getRepairOrderDetails().forEach(p->{
                p.setRepairOrder(repairOrder);
            });
            repairOrderDetailsDao.saveAll(repairOrder.getRepairOrderDetails());
        }
    }
    //根据客户id查询此客户下所有订单
    public List<Order> selectorder(Integer oid){
        return repairOrderMapper.selectorder(oid);
    };
    //根据订单id查询订单详情
    public List<OrderDetails> selectodte(Integer odet){
        return repairOrderMapper.selectodte(odet);
    };

    //客户签约人按条件查询
    public List<Linkman> selectlinkman(Integer ji){
        List<Linkman> linkmen=repairOrderMapper.selectqyr(ji);
        return linkmen;
    }
    //查询维修总工单
    public  Map<String,Object> selectzonkong(HttpSession session,Integer cid){
        cid=fouyuan(session);
        List<RepairOrder> repairOrderList=repairOrderMapper.selectdaiweix(cid);
        List<RepairOrder> repairOrderList2=repairOrderMapper.selectgoutong(cid);
        List<RepairOrder> repairOrderList3=repairOrderMapper.selectjiance(cid);
        List<RepairOrder> repairOrderList4=repairOrderMapper.selectjiaofu(cid);
        List<RepairOrder> repairOrderList5=repairOrderMapper.selectqrweix(cid);
        List<RepairOrder> repairOrderList6=repairOrderMapper.selectweishouk(cid);
        List<RepairOrder> repairOrderList7=repairOrderMapper.selectweixiu(cid);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("daiweixiu",repairOrderList);
        map.put("daigoutong",repairOrderList2);
        map.put("jiance",repairOrderList3);
        map.put("jiaofu",repairOrderList4);
        map.put("qrweix",repairOrderList5);
        map.put("weishouk",repairOrderList6);
        map.put("weixiu",repairOrderList7);
        map.put("rukudan","RK"+newDate+result);
        return map;
    }
    //修改维修进度
    public void  updatestate(String state,Integer rid){
        repairOrderMapper.updatestate(state,rid);
    }
    //修改此工单是否维修过状态
    public void updatweifou(Integer rid){
        repairOrderMapper.updatweifou(rid);
    };
    //详情
    public List<RepairOrderDetails> selectxian(Integer rid){
        return  repairOrderMapper.selectxian(rid);
    }
    //确认入库按钮改变维修入库状态未待入库
    public void updateweixx(String rukufou, Integer rid){
        repairOrderMapper.updateweixx(rukufou,rid);
    };
    //查询所有的维修记录和采购记录
    public List<Purchase> selectcaigou(){
        return  repairOrderMapper.selectcaigou();
    };
    public List<RepairOrder> selectweixiut(){
        return  repairOrderMapper.selectweixiut();
    };
}
